package com.mark.test;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;  
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;  
import static org.quartz.TriggerBuilder.newTrigger; 
/**
 * 手动执行单个job
 * @author maliqiang
 * @since 2016年12月7日
 */
public class RunSingleJob  {
	public static void executeJob() throws SchedulerException {
		// define the job and tie it to our MyJob class
		  JobDetail job = newJob(MyJob.class)
		      .withIdentity("job1", "group1")
		      .build();

		  // Trigger the job to run now, and then repeat every 40 seconds
		  Trigger trigger = newTrigger()
		      .withIdentity("trigger1", "group1")
		      .startNow()
		      .withSchedule(simpleSchedule()
		      .withIntervalInSeconds(1)
		      .repeatForever())
		      .build();
		  
		  
		  // Grab the Scheduler instance from the Factory
		  Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
		  // Tell quartz to schedule the job using our trigger
		  scheduler.scheduleJob(job, trigger);
		  // and start it off
		  scheduler.start();
	}
	
	
	public static void main(String[] args) throws SchedulerException {
		executeJob();
	}
}

class MyJob implements Job {

    public MyJob() {
    }
    /**
     * job execute method and logic
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("Hello World!  MyJob is executing.");
    }
}
